package com.haibei.activity.market;

import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.common.net.NetResult;
import com.common.task.BaseTask;
import com.common.task.NetCallBack;
import com.common.uitl.Tool;
import com.common.view.SegmentedRadioGroup;
import com.haibei.App;
import com.haibei.R;
import com.haibei.activity.BaseHaibeiActivity;
import com.haibei.activity.user.LoginActivity;
import com.haibei.adapter.GuiderImageAdapter;
import com.haibei.adapter.bean.Goods;
import com.haibei.net.NetInterface;
import com.haibei.view.CircleFlowIndicator;
import com.haibei.view.ViewFlow;

public class GoodsDetailActivity extends BaseHaibeiActivity implements OnClickListener {

	private ViewFlow mViewFlow;
	private SegmentedRadioGroup msegmentedRadioGroup;
	private LinearLayout msegmentInfoType1;
	private LinearLayout msegmentInfoType2;
	private LinearLayout msegmentInfoType3;
	private BaseTask mBaseTask;
	private Goods mGoods;
	private TextView mTextViewSologan;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// intent.putExtra("goods", gods);
		mGoods = (Goods) getIntent().getSerializableExtra("goods");
		initView();
	}

	@Override
	public void initView() {
		setContentView(R.layout.activity_goods_detail);
		msegmentInfoType1 = (LinearLayout) findViewById(R.id.segmentInfoType1);
		msegmentInfoType2 = (LinearLayout) findViewById(R.id.segmentInfoType2);
		msegmentInfoType3 = (LinearLayout) findViewById(R.id.segmentInfoType3);

		mViewFlow = (ViewFlow) findViewById(R.id.viewflow);
		mViewFlow.setAdapter(new GuiderImageAdapter(this));
		msegmentedRadioGroup = (SegmentedRadioGroup) findViewById(R.id.segmentInfoType);
		CircleFlowIndicator indic = (CircleFlowIndicator) findViewById(R.id.viewflowindic);// 小点点
		mViewFlow.setFlowIndicator(indic);
		findButtonById(R.id.buttonBack).setOnClickListener(this);
		findViewById(R.id.btnBuy).setOnClickListener(this);
		msegmentedRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.button_one:
					msegmentInfoType1.setVisibility(View.VISIBLE);
					msegmentInfoType2.setVisibility(View.INVISIBLE);
					msegmentInfoType3.setVisibility(View.INVISIBLE);
					break;
				case R.id.button_two:
					msegmentInfoType1.setVisibility(View.INVISIBLE);
					msegmentInfoType2.setVisibility(View.VISIBLE);
					msegmentInfoType3.setVisibility(View.INVISIBLE);
					break;
				case R.id.button_three:
					msegmentInfoType1.setVisibility(View.INVISIBLE);
					msegmentInfoType2.setVisibility(View.INVISIBLE);
					msegmentInfoType3.setVisibility(View.VISIBLE);
					break;
				}
			}
		});
		mTextViewSologan = findTextViewById(R.id.textViewSologan);
		mTextViewSologan.setText(mGoods!=null?mGoods.getSlogan():"");
		findButtonById(R.id.buttonCollect).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				collectWare();
			}
		});
		requestGoodsDetail();// 请求详情
	}

	// 填充详情信息
	private void fillDetailInfo(Goods detail, String commentCounts) {
		Button btnComment = findButtonById(R.id.buttonCommentCount);
		btnComment.setText(commentCounts);
		btnComment.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent();
				i.setClass(GoodsDetailActivity.this, GoodsCommentsActivity.class);
				i.putExtra("goods", mGoods);
				startActivity(i);
			}
		});
		// =========================
		TextView title = findTextViewById(R.id.textViewTite);
		title.setText(detail.getTitle());
		TextView subTitle = findTextViewById(R.id.textViewSubTitle);
		subTitle.setText(detail.getSubTitle());
		TextView textViewPrice = findTextViewById(R.id.textViewPrice);
		textViewPrice.setText("￥" + detail.getSalePrice());

		TextView textViewDetailInfro = findTextViewById(R.id.textViewDetailInfo);
		textViewDetailInfro.setText(detail.getDetailIntro());
		TextView textViewSpcification = findTextViewById(R.id.textViewSpecification);
		textViewSpcification.setText(detail.getSpecification());
		TextView textViewAfterSales = findTextViewById(R.id.textViewAfterSales);
		textViewAfterSales.setText(detail.getAfterScale());
		mTextViewSologan.setText(detail.getSlogan());
	}

	@Override
	public void onClick(View v) {
		int vid = v.getId();
		switch (vid) {
		case R.id.buttonBack: {
			finish();
		}
			break;
		case R.id.btnBuy: {
			Tool.startActivity(GoodsDetailActivity.this, ShopingActivity.class);
		}
			break;
		}
	}

	private void requestGoodsDetail() {
		HashMap<String, String> hmap = new HashMap<String, String>();
		hmap.put("wareId", mGoods.getId());
		getGoodsDetailTask().execute(hmap);
	}

	private void collectWare() {
		if (!App.isLogin()) {
			Tool.startOtherActivity(GoodsDetailActivity.this, LoginActivity.class);
			return;
		}
		// ServiceResult<Favorite> result = favoriteService.addWare(uid,
		// wareId);
		HashMap<String, String> hmap = new HashMap<String, String>();
		hmap.put("wareId", mGoods.getId());
		hmap.put("uid", App.getApp().getUser().getUid());
		getCollectTask().execute(hmap);
	}

	private BaseTask getGoodsDetailTask() {
		if (null != mBaseTask && !mBaseTask.isCancelled()) {
			mBaseTask.cancel(true);
		}
		mBaseTask = new BaseTask(new NetCallBack() {

			@Override
			public void onPreCall() {
				showProgressDialogWithTask(mBaseTask);
			}

			@Override
			public void onFinish(NetResult result) {
				hideProgressDialogWithTask();
				if (null != result) {
					if (result.isSuccess()) {
						Goods god = (Goods) result.getData()[0];
						mGoods = god;// 最新的商品详情信息
						fillDetailInfo(god, (String) result.getData()[1]);
					} else {
						Tool.ToastShow(GoodsDetailActivity.this, result.getMessage());
					}
				} else {
					Tool.ToastShow(GoodsDetailActivity.this, R.string.error_net);
				}
			}

			@Override
			public NetResult onDoInBack(HashMap<String, String> paramMap) {
				NetResult netReslt = null;
				try {
					netReslt = NetInterface.getGoodsDetail(GoodsDetailActivity.this, paramMap);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return netReslt;
			}
		});
		return mBaseTask;
	}

	private BaseTask getCollectTask() {
		if (null != mBaseTask && !mBaseTask.isCancelled()) {
			mBaseTask.cancel(true);
		}
		mBaseTask = new BaseTask(new NetCallBack() {

			@Override
			public void onPreCall() {
				showProgressDialogWithTask(mBaseTask);
			}

			@Override
			public void onFinish(NetResult result) {
				hideProgressDialogWithTask();
				if (null != result) {
					if (result.isSuccess()) {
						Tool.ToastShow(GoodsDetailActivity.this, "收藏成功！");
					} else {
						Tool.ToastShow(GoodsDetailActivity.this, result.getMessage());
					}
				} else {
					Tool.ToastShow(GoodsDetailActivity.this, R.string.error_net);
				}
			}

			@Override
			public NetResult onDoInBack(HashMap<String, String> paramMap) {
				NetResult netReslt = null;
				try {
					netReslt = NetInterface.collectGoods(GoodsDetailActivity.this, paramMap);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return netReslt;
			}
		});
		return mBaseTask;
	}
}
